<?php
namespace admin\service;

use admin\service\interfaces\IManagerService;
use common\service\CommonService;
use herosphp\core\Loader;

Loader::import('admin.service.interfaces.IManagerService');

/**
 * admin(Service)接口实现
 * @package admin\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class ManagerService extends CommonService implements IManagerService
{


    public function __construct($model)
    {
        parent::__construct($model);
    }

    public function getItem($condition, $fields, $order)
    {
        $condition['del_status'] = 0;
        $item = parent::getItem($condition, $fields, $order);
        if (!empty($item['role_id'])) {
            $model = Loader::model("ManagerRole");
            $role = $model->getItem($item['role_id']);
            $item['role_name'] = $role['name'];
        }
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        $conditions['del_status'] = 0;
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if (!empty($items)) {
            $ids = array();
            foreach ($items as $val) {
                $ids[] = $val['role_id'];
            }
            $model = Loader::model("ManagerRole");
            $roles = $model->getItems(array('id' => array('$in' => $ids)));
            $roles = ArrayUtils::changeArrayKey($roles, 'id');
            foreach ($items as $key => $value) {
                $items[$key]['role_name'] = $roles[$value['role_id']]['name'];
            }
        }

        return $items;
    }

    public function delete($id)
    {
        return parent::update(array('del_status' => 1), $id);
    }

    public function deletes($conditions)
    {
        return parent::updates(array('del_status' => 1), $conditions);
    }
}
